package com.hhkj.api.train.common.bizutils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.admin.question.common.converter.QuestionCollectionConverter;
import com.hhkj.admin.question.domain.Question;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.admin.question.service.base.QuestionService;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.domain.TrainStudentAnswer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * ApiTrainBizUtils
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/29 15:41
 */
@Component
public class ApiTrainBizUtils
{
    @Autowired
    private QuestionService questionService;

    /**
     * 匹配 练习题目信息
     *
     * @param train 培训信息
     */
    public void matchTrainQuestionList(Train train)
    {
        // 答案列表信息处理 获取题目id列表信息
        List<TrainStudentAnswer> studentAnswerList = train.getAnswerList();
        List<Long> questionIdList = studentAnswerList.stream()
                .map(TrainStudentAnswer::getQuestionId)
                .collect(Collectors.toList());

        // 获取对应的题目列表信息进行分组
        List<Question> questionList
                = questionService.listByIds(questionIdList);
        Map<Long, QuestionRes> sourceQuestionGroupMapById
                = questionList.stream()
                .map(po -> BeanUtil.toBean(po, QuestionRes.class))
                .collect(Collectors.toMap(QuestionRes::getId, Function.identity()));

        // 遍历答案列表信息，根据学员答案列表排序对应题目信息
        List<QuestionRes> correctQuestionList = new ArrayList<>();
        studentAnswerList.sort(Comparator.comparingInt(TrainStudentAnswer::getOrderNum));
        QuestionRes question;
        for (TrainStudentAnswer trainStudentAnswer : studentAnswerList) {
            if (sourceQuestionGroupMapById.containsKey(trainStudentAnswer.getQuestionId())) {
                question = sourceQuestionGroupMapById
                        .get(trainStudentAnswer.getQuestionId());
                question.setStudentAnswer(trainStudentAnswer.getStudentAnswer());
                question.setStudentMark(trainStudentAnswer.getStudentMark());
                question.setCorrectResCode(trainStudentAnswer.getCorrectResCode());
                question.setOrderNum(trainStudentAnswer.getOrderNum());

                if (CollectionUtil.isNotEmpty(trainStudentAnswer.getActualOptionOrderList())) {
                    question.reductionShuffleOptionList(trainStudentAnswer.getActualOptionOrderList());
                }

                question.setMark(trainStudentAnswer.getQuestionMark());
                correctQuestionList.add(question);
            }
        }

        // 培训信息设置题目组列表信息
        train.setQuestionGroupList(QuestionCollectionConverter.listToListGroup(correctQuestionList));
    }
}
